package cn.itcast.bos.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * 注入service
	 * @return
	 */
	@Autowired
	private StandardService standardService;
	
	@Action(value="standard_save",results={@Result(name="save",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		standardService.save(model);
		return "save";
	}
	
	@Action(value="standard_validateName",results={@Result(name="json",type="json")})
	public String validateName(){
		List<Standard> list = standardService.findByName(model.getName());
		if (list!=null&&list.size()==0) {
			ServletActionContext.getContext().getValueStack().push(true);
		}else {
			ServletActionContext.getContext().getValueStack().push(false);
		}
		
		return "json";
	}
	

	@Action(value="standard_pageQuery",results={@Result(name="json",type="json")})
	public String pageQuery(){
		//调用业务层,查询数据结果
		Pageable pageable = new PageRequest(page -1,rows);
		Page<Standard> pageDate = standardService.findPageDate(pageable);
		
		pushPageDataToValueStack(pageDate);
		return "json";
	}

	
	@Action(value="standard_deleteStandard",results={@Result(name="deleteSuccess",location="./pages/base/standard.html",type="redirect")})
	public String deleteStandard(){
		standardService.deleteByIds(ids);
		return "deleteSuccess";
	}
	
	@Action(value="standard_findAll",results={@Result(name="json",type="json")})
	public String findAll(){
		List<Standard> list = standardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return "json";
	}

}
